/**
 * Created by JetBrains WebStorm.
 * User: Tonis
 * Date: 25.02.12
 * Time: 21:04
 * To change this template use File | Settings | File Templates.
 */
var Mode = Class.extend({
    init:function (x, y, w, h, text, type, fill) {

        this.x = x || 0;
        this.y = y || 0;
        this.w = w || 1;
        this.h = h || 1;
        this.fill = fill || '#AAAAAA';
        this.text = text || "Default";
        this.type = type || GameModeSelection.PLAYERvsPLAYER;
        this.border = false;
    },
    draw:function () {
        ctx.fillStyle = this.fill;
        ctx.fillRect(this.x, this.y, this.w, this.h);
        ctx.fillStyle = "#000000";
        ctx.font="30px Arial";
        ctx.fillText(this.text, this.x + 10, this.y + (this.h /2) + 10);
        if (this.border){
            ctx.strokeStyle = "#FF0000";
            ctx.strokeRect(this.x-1, this.y-1, this.w+1, this.h+1);

        }
    },
    setBorder: function(border){
        this.border = border;
    },
    isWithinBounds:function (mx, my) {
        return  (this.x <= mx) && (this.x + this.w >= mx) &&
            (this.y <= my) && (this.y + this.h >= my);
    }
});

var GameModes = Class.extend({
    gameModeList: new Array(),
    init:function () {
        console.log("INIT");
        canvas.addEventListener('mousedown', mouseDown, false);
        canvas.addEventListener('mousemove', mouseMove, false);
    },
    addMode:function (mode) {
        this.gameModeList.push(mode);
    },
    getGameModeList: function(){
        return this.gameModeList;
    },
    draw:function () {
        for (var i = 0; i < this.gameModeList.length; i++) {
            this.gameModeList[i].draw();
        }
    }
});


function mouseDown(e) {
    var gameModeList = gameModes.getGameModeList();
    var mouse = getMouse(e);
    var mx = mouse.x;
    var my = mouse.y;
    //alert(gameModeList.length);
    var l = gameModeList.length;
    for (var i = l - 1; i >= 0; i--) {
        if (gameModeList[i].isWithinBounds(mx, my)) {
            gameModeSelected = true;
            switch (gameModeList[i].type){
                case GameModeSelection.PLAYERvsPLAYER:
                    initTwoPlayerGame();
                    canvas.removeEventListener('mousedown', mouseDown, false);
                    canvas.removeEventListener('mousemove', mouseMove, false);
                    break;
                case GameModeSelection.PLAYERvsAI:
                    initOnePlayerGame();
                    canvas.removeEventListener('mousedown', mouseDown, false);
                    canvas.removeEventListener('mousemove', mouseMove, false);
                    break;
                case GameModeSelection.MultiPLAYER:
                    initTwoPlayerGame();
                    canvas.removeEventListener('mousedown', mouseDown, false);
                    canvas.removeEventListener('mousemove', mouseMove, false);
                    break;
                default:
                    break;
            }
        }
    }
}

function  mouseMove(e) {
    var gameModeList = gameModes.getGameModeList();
    var mouse = getMouse(e);
    var mx = mouse.x;
    var my = mouse.y
    var l = gameModeList.length;
    for (var i = l - 1; i >= 0; i--) {
        if (gameModeList[i].isWithinBounds(mx, my)) {
            gameModeList[i].setBorder(true);

        }else{
            gameModeList[i].setBorder(false);
        }
    }

}
